package com.kelp.codetoolkit.listener

 import com.intellij.openapi.fileEditor.FileEditorManager
 import com.intellij.openapi.fileEditor.FileEditorManagerEvent
 import com.intellij.openapi.fileEditor.FileEditorManagerListener
 import com.intellij.openapi.project.Project
 import com.intellij.openapi.vfs.VirtualFile
 import org.yaml.snakeyaml.Yaml
 import java.io.StringReader


/**
 * @author: liuyuebai
 * @date: 2025/9/8 15:05
 * @description: TextEditorComponent 是 IDEA 内部用来显示源码文本的组件，它不是一个通用的 Swing 容器
 */
class SpringConfigSelectionListener(private val project: Project): FileEditorManagerListener {

    private val yamlParser = Yaml()

    override fun selectionChanged(event: FileEditorManagerEvent) {
        val newFile = event.newFile
        val extension = newFile?.extension
        if (extension !in arrayOf("yml", "yaml", "properties")) {
            return
        }
        when (extension) {
            "yml", "yaml" -> {
                val ymlMap = parseYamlToMap(newFile)
            }
            "properties" -> {}
        }
    }

    /**
     * 解析 YAML 文件并转换为 Map 结构
     */
    private fun parseYamlToMap(file: VirtualFile): Map<String, Any> {
        return try {
            // 读取文件内容
            val content = String(file.contentsToByteArray())
            // 使用 SnakeYAML 解析 YAML 内容
            return yamlParser.load<Map<String, Any>>(StringReader(content)) as? Map<String, Any> ?: emptyMap()
        } catch (e: Exception) {
            emptyMap()
        }
    }

    private fun show(file: VirtualFile){
        val fileEditorManager = FileEditorManager.getInstance(project)

    }
}